import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/api/home.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_list.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_nav.dart';
import 'package:flutter_enjoy_plus/utils/Toast.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<HomePage> {
  // 获取社区公告列表
  List annoucementList = [];
  @override
  void initState() {
    super.initState();
    getAnnounceList();
  }

  // 获取公告列表
  getAnnounceList() async {
    final res = await getAnnounceListAPI();
    PromptAction.showToast('获取公告列表成功');
    setState(() {
      annoucementList = res;
    });
    print(res);
  }

  Timer? timer;
  int _count_down = 60;

  // 封装的List Widget -> 操作token
  Widget getListToken() {
    return Row(
      children: [
        Expanded(
          child: ElevatedButton(
            onPressed: () {},
            child: const Text('存储token'),
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: ElevatedButton(
            onPressed: () {
              timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                _count_down--;
                setState(() {});
              });
            },
            child: const Text('开启定时器'),
          ),
        ),
        Text('剩余: $_count_down 秒'),
        Expanded(
          child: ElevatedButton(
            onPressed: () {
              timer?.cancel();
            },
            child: const Text('关闭定时器'),
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: ElevatedButton(
            onPressed: () {
              Future.delayed(const Duration(seconds: 3), () {
                PromptAction.showToast('延时器');
              });
            },
            child: const Text('延时器'),
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color.fromRGBO(229, 231, 241, 1),
        appBar: AppBar(
          title: const Text('享+社区'),
          centerTitle: true,
          backgroundColor: Colors.transparent,
        ),
        body: ListView(
          padding: const EdgeInsets.all(10),
          children: [
            // getListToken(),
            // 导航条
            HomeNav(),
            // 轮播图
            ClipRRect(
                borderRadius: BorderRadius.circular(10),
                child: Image.asset('assets/images/banner@2x.jpg')),
            // 社区公告
            HomeList(notifyList: annoucementList)
          ],
        ));
  }
}
